package donot996.icu.tieba.controller;

import donot996.icu.tieba.domain.DetailList;
import donot996.icu.tieba.domain.TopicList;
import donot996.icu.tieba.response.JsonResult;
import donot996.icu.tieba.service.IDetailListService;
import donot996.icu.tieba.service.ITopicListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/topic")
public class TopicListController {
    @Autowired
    ITopicListService topicListService;

    @Autowired
    IDetailListService detailListService;

    // 获取所有话题
    @GetMapping("/topicList")
    public JsonResult<List<TopicList>> topicList(){
        List<TopicList> list = topicListService.getListInfo();
        return  JsonResult.success(list);
    }

    @GetMapping("/topicDetail")
    public JsonResult<List<TopicList>> getTopic(Long id){
        TopicList topic = topicListService.getById(id);
        String[] detailIds  = topic.getDetailIds().split(",");
        List<DetailList> detailLists = detailListService.listByIds(Arrays.asList(detailIds));
        topic.setDetailList(detailLists);
        return  JsonResult.success(topic);
    }

    // 发起一个话题
    @PostMapping("/submitTopic")
    public JsonResult submitTopic(Long userId, String userName, String content){
        topicListService.submitTopic(userId, userName, content);
        return  JsonResult.success();
    }
}
